//-----------------------------------------------------------------------
// <copyright file="BannerLocationDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class BannerLocationDAOBase
    {
        #region Common methods
        public virtual BannerLocation CreateBannerLocationFromReader(IDataReader reader)
        {
            BannerLocation item = new BannerLocation();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("BannerLocationId"))) item.BannerLocationId = (long)reader["BannerLocationId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatLocationId"))) item.CatLocationId = (long)reader["CatLocationId"];
                if (!reader.IsDBNull(reader.GetOrdinal("SizeID"))) item.SizeID = (long)reader["SizeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("Price1"))) item.Price1 = (long)reader["Price1"];
                if (!reader.IsDBNull(reader.GetOrdinal("Price2"))) item.Price2 = (long)reader["Price2"];
                if (!reader.IsDBNull(reader.GetOrdinal("UnitPriceId"))) item.UnitPriceId = (int)reader["UnitPriceId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CurrentQuantity"))) item.CurrentQuantity = (int)reader["CurrentQuantity"];
                if (!reader.IsDBNull(reader.GetOrdinal("Duration"))) item.Duration = (int)reader["Duration"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartusId"))) item.StartusId = (int)reader["StartusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerLocationFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateBannerLocation methods
            
        public virtual void CreateBannerLocation(BannerLocation bannerLocation)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerLocationInsert");
                
                database.AddInParameter(dbCommand, "@CatLocationId", DbType.Int64, bannerLocation.CatLocationId);
                database.AddInParameter(dbCommand, "@SizeID", DbType.Int64, bannerLocation.SizeID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, bannerLocation.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, bannerLocation.Description);
                database.AddInParameter(dbCommand, "@Price1", DbType.Int64, bannerLocation.Price1);
                database.AddInParameter(dbCommand, "@Price2", DbType.Int64, bannerLocation.Price2);
                database.AddInParameter(dbCommand, "@UnitPriceId", DbType.Int32, bannerLocation.UnitPriceId);
                database.AddInParameter(dbCommand, "@CurrentQuantity", DbType.Int32, bannerLocation.CurrentQuantity);
                database.AddInParameter(dbCommand, "@Duration", DbType.Int32, bannerLocation.Duration);
                database.AddInParameter(dbCommand, "@StartusId", DbType.Int32, bannerLocation.StartusId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, bannerLocation.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, bannerLocation.Enable);
                database.AddOutParameter(dbCommand, "@BannerLocationId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                bannerLocation.BannerLocationId = (long)database.GetParameterValue(dbCommand, "@BannerLocationId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerLocationException, ex);
            }
        }

        #endregion

        #region UpdateBannerLocation methods
        
        public virtual void UpdateBannerLocation(BannerLocation bannerLocation)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerLocationUpdate");            
                
                database.AddInParameter(dbCommand, "@BannerLocationId", DbType.Int64, bannerLocation.BannerLocationId);
                database.AddInParameter(dbCommand, "@CatLocationId", DbType.Int64, bannerLocation.CatLocationId);
                database.AddInParameter(dbCommand, "@SizeID", DbType.Int64, bannerLocation.SizeID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, bannerLocation.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, bannerLocation.Description);
                database.AddInParameter(dbCommand, "@Price1", DbType.Int64, bannerLocation.Price1);
                database.AddInParameter(dbCommand, "@Price2", DbType.Int64, bannerLocation.Price2);
                database.AddInParameter(dbCommand, "@UnitPriceId", DbType.Int32, bannerLocation.UnitPriceId);
                database.AddInParameter(dbCommand, "@CurrentQuantity", DbType.Int32, bannerLocation.CurrentQuantity);
                database.AddInParameter(dbCommand, "@Duration", DbType.Int32, bannerLocation.Duration);
                database.AddInParameter(dbCommand, "@StartusId", DbType.Int32, bannerLocation.StartusId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, bannerLocation.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, bannerLocation.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateBannerLocationException, ex);
            }
        }
        
        #endregion

        #region DeleteBannerLocation methods
        public virtual void DeleteBannerLocation(long bannerLocationId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerLocationDelete");
                
                database.AddInParameter(dbCommand, "@BannerLocationId", DbType.Int64, bannerLocationId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteBannerLocationException, ex);
            }
        }

        #endregion

        #region GetBannerLocation methods
        
        public virtual BannerLocation GetBannerLocation(long bannerLocationId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerLocationGet");
                
                database.AddInParameter(dbCommand, "@BannerLocationId", DbType.Int64, bannerLocationId);
                
                BannerLocation bannerLocation =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        bannerLocation = CreateBannerLocationFromReader(reader);
                        reader.Close();
                    }
                }
                return bannerLocation;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerLocationException, ex);
            }
        }
        
        #endregion

        #region GetBannerLocationList methods
        public virtual BannerLocationCollection GetBannerLocationList(BannerLocationColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerLocationGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerLocationCollection bannerLocationCollection = new BannerLocationCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        BannerLocation bannerLocation = CreateBannerLocationFromReader(reader);
                        bannerLocationCollection.Add(bannerLocation);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerLocationCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerLocationListException, ex);
            }
        }
        
        public virtual BannerLocationCollection GetBannerLocationList(BannerLocationColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetBannerLocationList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetBannerLocationList methods
        public virtual BannerLocationCollection GetBannerLocationListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerLocationGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerLocationCollection bannerLocationCollection = new BannerLocationCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        BannerLocation bannerLocation = CreateBannerLocationFromReader(reader);
                        bannerLocationCollection.Add(bannerLocation);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerLocationCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerLocationListException, ex);
            }
        }
        
        public virtual BannerLocationCollection GetBannerLocationListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetBannerLocationListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}